package com.lanqiao;

import java.io.*;
import java.util.ArrayList;

public class Text {
    static ArrayList<ArrayList<String>> arrayList ;
    public static void main(String[] args) throws Exception {
        String url = "F:\\" ;//请传入地址
        inFile(url);
        copy(url);




    }
    //开始复制
    public static void copy(String url) throws Exception {
        arrayList = urlAndName(url) ;//
        for (int i = 0; i <arrayList.size() ; i++) {
            ioFile( arrayList.get(i).get(0)+"",url+"\\"+arrayList.get(i).get(2)+"文件\\"+(arrayList.get(i).get(3)));
        }
    }
    //用于创造装各类文件的文件夹
    public static void myMkdir(String url){
        File file = new File(url);
        System.out.println(url);
        File[] files = file.listFiles();
        String[] split;
        int a = 0 ;
        boolean exists = false;
        for (File file1 :
                files) {
            split = file1.getName().split("\\.");
            if (file1.isFile()){
                File file2 = new File(url+"\\"+split[split.length-1]+"文件") ;

                if (!file2.exists()){
                    file2.mkdir();

                }

            }

        }

    }
    //获取全部的原地址,后缀，文件名字
    public static  ArrayList<ArrayList<String>> urlAndName(String url){//传入

        File file = new File(url);
        File[] files = file.listFiles();
        ArrayList<ArrayList<String>> arrayList = new ArrayList<>();
        ArrayList<String> arrayList1 ;

        for (File fie :
                files) {
            String[] split = fie.getName().split("\\.");

            if (fie.isFile()){
                arrayList1 = new ArrayList<>() ;
                arrayList1.add(fie.getAbsolutePath()) ;//地址 0
                arrayList1.add(split[0]);//名字 1
                arrayList1.add(split[split.length-1]);//后缀 2
                arrayList1.add(fie.getName()) ;//名字 3
                arrayList.add(arrayList1);

            }


        }
        return arrayList ;
    }

    //用于各类资源的传输
    public static void ioFile(String oldFile,String newFile) throws Exception {
        InputStream inputStream = new FileInputStream(oldFile);
        OutputStream fileOutputStream = new FileOutputStream(newFile);
        byte[] bytes = new byte[1024];
        int tp ;
        while ((tp=inputStream.read(bytes))!=-1){
            fileOutputStream.write(bytes,0,tp);
        }
        inputStream.close();
        fileOutputStream.close();
        File file = new File(oldFile);
        file.delete() ;
    }
    //用于检测文件夹中的文件
    public static void inFile(String url) throws Exception {
        File file = new File(url);
        File[] files = file.listFiles();
        for (File f :
                files) {
            if (f.isDirectory()){
                String name = f.getName();
                inFile(url+"/"+name);

            }else {
                myMkdir(url);//产生装各类文件的文件夹
                copy(url);
            }
        }
    }
}
